package cz.data.domain.metadata.controller;

import cz.data.common.base.BaseController;
import cz.data.common.core.DataConstant;
import cz.data.common.annotation.DataInner;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import cz.data.domain.metadata.model.entity.MetadataAuthorizeEntity;
import cz.data.domain.metadata.model.entity.MetadataColumnEntity;
import cz.data.domain.metadata.model.entity.MetadataSourceEntity;
import cz.data.domain.metadata.model.entity.MetadataTableEntity;
import cz.data.domain.metadata.model.query.MetadataTableQuery;
import cz.data.domain.metadata.service.MetadataAuthorizeService;
import cz.data.domain.metadata.service.MetadataColumnService;
import cz.data.domain.metadata.service.MetadataSourceService;
import cz.data.domain.metadata.service.MetadataTableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/inner")
public class InnerController extends BaseController {

    @Autowired
    private MetadataSourceService metadataSourceService;
    @Autowired
    private MetadataTableService metadataTableService;
    @Autowired
    private MetadataColumnService metadataColumnService;
    @Autowired
    private MetadataAuthorizeService metadataAuthorizeService;

    @DataInner
    @GetMapping("/sources/{id}")
    public MetadataSourceEntity getMetadataSourceById(@PathVariable("id") String id) {
        return metadataSourceService.getMetadataSourceById(id);
    }

    @DataInner
    @GetMapping("/sources/list")
    public List<MetadataSourceEntity> getMetadataSourceList() {
        QueryWrapper<MetadataSourceEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", DataConstant.EnableState.ENABLE.getKey());
        return metadataSourceService.list(queryWrapper);
    }

    @DataInner
    @GetMapping("/tables/{id}")
    public MetadataTableEntity getMetadataTableById(@PathVariable("id") String id) {
        return metadataTableService.getMetadataTableById(id);
    }

    @DataInner
    @GetMapping("/tables/source/{id}")
    public List<MetadataTableEntity> getMetadataTableBySourceId(@PathVariable("id") String id) {
        return metadataTableService.getDataMetadataTableList(MetadataTableQuery.formSourceId(id));
    }

    @DataInner
    @GetMapping("/tables/list")
    public List<MetadataTableEntity> getMetadataTableList() {
        QueryWrapper<MetadataTableEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", DataConstant.EnableState.ENABLE.getKey());
        return metadataTableService.list(queryWrapper);
    }

    @DataInner
    @GetMapping("/columns/table/{id}")
    public List<MetadataColumnEntity> getMetadataColumnByTableId(@PathVariable("id") String id) {
        return metadataColumnService.getMetadataColumnByTableId(id);
    }

    @DataInner
    @GetMapping("/authorize/table/{role}")
    public List<MetadataAuthorizeEntity> getMetadataAuthorizeByRoleId(@PathVariable("role") String role) {
        return metadataAuthorizeService.getMetadataAuthorizeByRoleId(role);
    }
}
